//
//  HKDTabBarController.m
//  LiveCircle
//
//  Created by Perfector on 15/11/21.
//  Copyright © 2015年 Perfector. All rights reserved.
//

#import "HKDTabBarController.h"
#import "HKDButton.h"
#import "HKDBaseButton.h"
#define kTabBarHeight 49
#define kTopBarHeight 44

@interface HKDTabBarController ()
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIImageView *imageView;

@end

@implementation HKDTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    创建底部tabBarView
    _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight - kTabBarHeight, kScreenWidth, kTabBarHeight)];
//    [_bottomView setBackgroundColor:[UIColor redColor]];

    [self.view addSubview:_bottomView];
    
    
//    创建底部上面的tabBarView
    _topView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight - kTabBarHeight - kTopBarHeight , kScreenWidth, kTopBarHeight)];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:_topView.bounds];
    imageView.image = [UIImage imageNamed:@"home_topbar"];
    _imageView = imageView;
    [_topView addSubview:_imageView];
    
    _topView.hidden = YES;

    [self.view addSubview:_topView];
    [self  addBtn];
    [self addTopBtn];

 
}
#pragma mark 增加tabBar按钮
-(void)addBtn{
    for (int i = 0; i < 5; i++ ) {
//        拼接按钮背景图片Name
        NSString *imageName = [NSString stringWithFormat:@"home_%d",i];
        NSString *imageNamePress = [NSString stringWithFormat:@"home_%d_pressed",i];
        HKDButton *btn = [HKDButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i * kScreenWidth / 5.0, 0, kScreenWidth / 5.0, kTabBarHeight);
        [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:imageNamePress] forState:UIControlStateSelected];
        btn.tag = i;

//        更多
        if (i == 0) {
            btn.selected = YES;
        }
        
    
        [_bottomView addSubview:btn];
        [btn addTarget:self action:@selector(clickChange:) forControlEvents:UIControlEventTouchUpInside];
    }
 
}

#pragma mark 增加顶部tabBar按钮

-(void)addTopBtn{
    NSArray *titleName = @[@"社区服务",@"生活周边",@"周边",@"收起"];
    NSArray *distance = @[@(0),@(160/640.0),@(340/640.0),@(475/640.0),@(1)];
    NSLog(@"%@",titleName);
    for (int i = 0; i < titleName.count; i++ ) {
        
        UIButton *btn = [HKDBaseButton creatButtonWithType:0 andWithTitle:titleName[i] andWithTarget:self andWithCGFrame:CGRectMake(kScreenWidth * [distance[i] floatValue], 0, kScreenWidth * ([distance[i + 1] floatValue] - [distance[i] floatValue]), kTopBarHeight) andWithAction:@selector(clickChange:)];

        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        btn.tag = 5 + i;
        [_topView addSubview:btn];
    }
  
}

-(void)clickChange:(HKDButton *)btn{
    
    _topView.hidden = YES;
    NSLog(@"%ld",btn.tag);

//    移除上一个按钮的选择状态
   [ _bottomView.subviews enumerateObjectsUsingBlock:^(__kindof UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       obj.selected = NO;
   }];
    btn.selected = YES;

    NSLog(@"%ld",self.selectedIndex);

    if (btn.tag < 4) {
    self.selectedIndex = btn.tag;
        _topView.hidden = YES;
    }else if(btn.tag == 4){
    
        _topView.hidden = NO;

    }else{
    
        self.selectedIndex = btn.tag - 1;

    }
    
    
}

@end
